package com.ddone.sms.controller;

import com.ddone.common.exception.Assert;
import com.ddone.common.exception.BusinessException;
import com.ddone.common.result.Response;
import com.ddone.common.result.ResponseEnum;
import com.ddone.sms.feign.UserInfoClient;
import com.ddone.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author ddone
 * @date 2024/4/15-18:58
 */
@Api(tags = "短信发送模块")
@RestController
@RequestMapping("/api/sms")
// @CrossOrigin
@Slf4j
public class SmsController {

    @Resource
    private SmsService smsService;

    @Resource
    private UserInfoClient userInfoClient;

    @ApiOperation("给对应类型的业务发送验证码,需要手机号和业务类型")
    @GetMapping("/{mobile}/{type}")
    public Response<Object> send(
            @ApiParam("手机号")
            @PathVariable(value = "mobile")
            String mobile,
            @ApiParam(value = "业务类型",required = true,example = "1")
            @PathVariable("type")
            Integer type) {
        Response<Object> clientResponse = userInfoClient.checkMobile(mobile);
       if(clientResponse.getData() == null){
           throw new BusinessException(ResponseEnum.FEIGN_REMOTE_ERROR);
       }
        boolean isRegister = (Boolean)clientResponse.getData();
        Assert.isFalse(isRegister, ResponseEnum.MOBILE_EXIST_ERROR);
        smsService.sendSms(mobile,type);
        return Response.ok().message("短信发送成功");
    }

}
